package com.story.aistory.service.impl;

import com.story.aistory.domain.po.Page;
import com.story.aistory.domain.po.Result;
import com.story.aistory.domain.po.Story;
import com.story.aistory.enums.AppHttpCodeEnum;
import com.story.aistory.mapper.PageMapper;
import com.story.aistory.service.IImagesService;
import com.story.aistory.service.IPageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hui
 * @since 2025-03-12
 */
@Service
@RequiredArgsConstructor
public class PageServiceImpl extends ServiceImpl<PageMapper, Page> implements IPageService {

    private final IImagesService imagesService;
    @Override
    public Result uploadPhoto(MultipartFile mpFile, Integer pageId) {
        String image = imagesService.uploadImage(mpFile, "images");
        if (image == null) {
            return Result.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);
        }
        Page page = new Page();
        page.setId(pageId);
        page.setPicUrl(image);
        updateById(page);
        return Result.success(image);
    }

    @Override
    public boolean saveList(List<Page> pageList) {
//        TODO
        return saveBatch(pageList);
    }

    @Override
    public List<Page> ListByStoryId(Integer storyId) {
        return this.lambdaQuery().eq(Page::getStoryId, storyId).groupBy(Page::getId).list();
    }

    @Override
    public List<Page> ListByStoryIds(Set<Integer> storyIds) {
        return this.lambdaQuery().in(Page::getStoryId, storyIds).groupBy(Page::getId).list();
    }

    @Override
    public void deleteByStoryId(Integer storyId) {
        this.lambdaUpdate().eq(Page::getStoryId, storyId).remove();
    }

    @Override
    public String uploadImage(MultipartFile multipartFile, String folder) {
        return imagesService.uploadImage(multipartFile, folder);
    }
}
